package com.epam.tcs.dao.hibernate.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.epam.tcs.dao.factory.PracticeDAO;
import com.epam.tcs.dao.hibernate.ConnectionFactory;
import com.epam.tcs.dao.hibernate.entities.Practice;

public class PracticeDAOHibernateImpl extends ElementDAOHibernateImpl<Practice>
		implements PracticeDAO {

	public PracticeDAOHibernateImpl(Class objType) {
		this.objType = objType;
	}

	@Override
	public Practice getElement(long id) {
		Session session = ConnectionFactory.getOrInitSession();
		Practice practice = (Practice) session.load(objType, (int)id);

		Hibernate.initialize(practice.getJournalPracticeStatuses());
		Hibernate.initialize(practice.getTheme());

		session.close();
		return practice;
	}

	@Override
	public List<Practice> getAllElements() {
		Session session = ConnectionFactory.getOrInitSession();
		List<Practice> practices = (List<Practice>) session.createQuery(
				"from " + objType.getName()).list();

		for (Practice practice : practices) {
			Hibernate.initialize(practice.getJournalPracticeStatuses());
			Hibernate.initialize(practice.getTheme());
		}

		session.close();
		return practices;
	}

}